package io.unicorn.modules.water.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.R;
import io.unicorn.common.validator.ValidatorUtils;
import io.unicorn.modules.water.service.DataErrFixedInfoService;
import io.unicorn.modules.waterfgs.entity.DataErrFixedInfoFgsEntity;
import io.unicorn.modules.watersub.entity.DataErrEntity;
import io.unicorn.modules.watersub.service.DataErrService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * ${comments}
 *
 * @author LNCG
 * @email LNCG@USTL.EDU.CN
 * @date 2023-04-01 09:44:54
 */
@RestController
@RequestMapping("water/dataerrfixedinfo")
public class DataErrFixedInfoController {
    @Autowired
    private DataErrService dataErrService;

    @Autowired
    private DataErrFixedInfoService dataErrFixedInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("water:dataerrfixedinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dataErrFixedInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{opKey}")
    @RequiresPermissions("water:dataerrfixedinfo:info")
    public R info(@PathVariable("opKey") Integer opKey){
        DataErrFixedInfoFgsEntity dataErrFixedInfo = dataErrFixedInfoService.getById(opKey);

        return R.ok().put("dataErrFixedInfo", dataErrFixedInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("water:dataerrfixedinfo:save")
    public R save(@RequestBody DataErrFixedInfoFgsEntity dataErrFixedInfo){
        dataErrFixedInfoService.save(dataErrFixedInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("water:dataerrfixedinfo:update")
    public R update(@RequestBody DataErrFixedInfoFgsEntity dataErrFixedInfo){
        ValidatorUtils.validateEntity(dataErrFixedInfo);
        dataErrFixedInfoService.updateById(dataErrFixedInfo);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("water:dataerrfixedinfo:delete")
    public R delete(@RequestBody Integer[] opKeys){
        dataErrFixedInfoService.removeByIds(Arrays.asList(opKeys));
        return R.ok();
    }

    @RequestMapping("/delFixed")
    @RequiresPermissions("water:dataerrfixedinfo:delete")
    public R deleteFixed(@RequestBody Integer[] opKeys){
        Arrays.stream(opKeys).forEach(obj->{
            dataErrService.lambdaUpdate()
                    .set(DataErrEntity::getDelFlag,0)
                    .eq(DataErrEntity::getDeviceId,dataErrFixedInfoService.getById(obj).getDeviceId())
                    .update();
        });

        dataErrFixedInfoService.removeByIds(Arrays.asList(opKeys));

        //
        return R.ok();
    }

    @RequestMapping("/fixed")
    @RequiresPermissions("water:dataerrfixedinfo:delete")
    public R fixed(@RequestBody String[] deviceIds){
        //根据id和date读取数据，存储到待处理列表
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String[] stringDeviceId = new String[deviceIds.length];
        //List<DataErrFixedInfoEntity> listDataErrFixedInfo = new ArrayList<>();
        for(int i = 0; i < deviceIds.length; i++){
            stringDeviceId[i] = deviceIds[i].split("_")[0];
            DataErrEntity dataErrEntity = dataErrService.getOne(
                    new QueryWrapper<DataErrEntity>()
                            .eq("device_id",stringDeviceId[i] )
                            .eq("now_date",deviceIds[i].split("_")[1])
                    ,true);

            DataErrFixedInfoFgsEntity dataErrFixedInfoEntity = new DataErrFixedInfoFgsEntity();
            dataErrFixedInfoEntity.setOpAddDate(format.format(new Date()));

            dataErrFixedInfoEntity.setDeviceId(dataErrEntity.getDeviceId());
            dataErrFixedInfoEntity.setNowDate(dataErrEntity.getNowDate());
            dataErrFixedInfoEntity.setNowData(dataErrEntity.getNowData());
            dataErrFixedInfoEntity.setDeviceStatus(dataErrEntity.getDeviceStatus());
            dataErrFixedInfoEntity.setAgoData(dataErrEntity.getAgoData());
            dataErrFixedInfoEntity.setAgoDeviceStatus(dataErrEntity.getAgoDeviceStatus());
            dataErrFixedInfoEntity.setProName(dataErrEntity.getProName());
            dataErrFixedInfoEntity.setDelFlag(dataErrEntity.getDelFlag());
            dataErrFixedInfoEntity.setMemo1(dataErrEntity.getMemo1());
            dataErrFixedInfoEntity.setMemo2(dataErrEntity.getMemo2());
            dataErrFixedInfoEntity.setMemo3(dataErrEntity.getMemo3());

            //listDataErrFixedInfo.add(dataErrFixedInfoEntity);
            dataErrFixedInfoService.save(dataErrFixedInfoEntity);

        }

        //dataErrFixedInfoService.saveOrUpdateBatch(listDataErrFixedInfo);

        //逻辑删除历史报警记录，该id的报警都标记删除
        dataErrService.removeByIds(Arrays.asList(stringDeviceId));

        return R.ok();
    }

}
